﻿/*==============================
版本：v0.1
创建：2016.3.10
作者：洞庭夕照
博客:http://mzwhj.cnblogs.com
==============================*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ninesky.Web.Models;
using System.Web.Mvc;
using Ninesky.Core;
using Ninesky.Auxiliary;

namespace Ninesky.Web.Areas.Control.Controllers
{
    [AdminAuthorize]
    public class RoleController : Controller
    {
        private RoleManager roleManager = new RoleManager();

        /// <summary>
        ///  添加
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            return PartialView();
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="role">角色</param>
        /// <returns>返回Json（Response）</returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Add(Role role)
        {
            Response _res = new Response();
            if (ModelState.IsValid) _res = roleManager.Add(role);
            else
            {
                _res.Code = 0;
                _res.Message = General.GetModelErrorString(ModelState);
            }
            return Json(_res);
        }

        /// <summary>
        /// 删除【Json】
        /// </summary>
        /// <param name="id">RoleID</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Delete(int id)
        {
            return Json(roleManager.Delete(id));
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        ///  列表【Json】
        /// </summary>
        /// <returns></returns>
        public JsonResult List()
        {
            return Json(roleManager.FindList());
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id">RoleID</param>
        /// <returns></returns>
        public ActionResult Modify(int id)
        {
            var _role = roleManager.Find(id);
            if(_role == null) return View("Prompt", new Prompt()
            {
                Title = "错误",
                Message = "ID为【" + id + "】的角色不存在",
                Buttons = new List<string>() { "<a href=\"" + Url.Action("Index", "Role") + "\" class=\"btn btn-default\">角色管理</a>"}
            });
            return PartialView(_role);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Modify(Role role)
        {

            Response _resp = new Auxiliary.Response();
            if (ModelState.IsValid)
            {
                _resp = roleManager.Update(role);
            }
            else
            {
                _resp.Code = 0;
                _resp.Message = General.GetModelErrorString(ModelState);
            }
            return Json(_resp);
        }

    }
}